<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.keyword"
        placeholder="请输入关键字"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select
        v-model="listQuery.status"
        placeholder="状态"
        clearable
        style="width: 120px"
        class="filter-item"
      >
        <el-option
          v-for="item in statusOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreate"
      >
        新增
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column
        label="职位名称"
        prop="name"
        align="center"
        width="200"
      />
      <el-table-column
        label="部门"
        prop="departmentName"
        align="center"
        width="150"
      />
      <el-table-column
        label="招聘人数"
        prop="count"
        align="center"
        width="100"
      />
      <el-table-column
        label="工作经验"
        prop="experience"
        align="center"
        width="120"
      />
      <el-table-column
        label="学历要求"
        prop="education"
        align="center"
        width="120"
      />
      <el-table-column
        label="薪资范围"
        prop="salary"
        align="center"
        width="150"
      />
      <el-table-column
        label="状态"
        prop="status"
        align="center"
        width="100"
      >
        <template #default="{row}">
          <el-tag :type="row.status === 1 ? 'success' : 'info'">
            {{ row.status === 1 ? '招聘中' : '已结束' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="发布时间"
        prop="createTime"
        align="center"
        width="180"
      />
      <el-table-column
        label="操作"
        align="center"
        width="200"
      >
        <template #default="{row}">
          <el-button
            size="mini"
            type="primary"
            @click="handleUpdate(row)"
          >
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { defineComponent, ref, onMounted } from 'vue'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'

export default defineComponent({
  name: 'PositionList',
  components: { Pagination },
  directives: { waves },
  setup() {
    const list = ref([])
    const total = ref(0)
    const listLoading = ref(true)
    const listQuery = ref({
      page: 1,
      limit: 20,
      keyword: '',
      status: undefined
    })

    const statusOptions = [
      { label: '招聘中', value: 1 },
      { label: '已结束', value: 0 }
    ]

    const getList = () => {
      listLoading.value = true
      // TODO: 调用API获取职位列表数据
      setTimeout(() => {
        list.value = [
          {
            id: 1,
            name: '前端开发工程师',
            departmentName: '技术部',
            count: 2,
            experience: '3-5年',
            education: '本科',
            salary: '15k-25k',
            status: 1,
            createTime: '2024-01-01 00:00:00'
          }
        ]
        total.value = 1
        listLoading.value = false
      }, 500)
    }

    const handleFilter = () => {
      listQuery.value.page = 1
      getList()
    }

    const handleCreate = () => {
      // TODO: 跳转到新增页面
    }

    const handleUpdate = (row) => {
      // TODO: 跳转到编辑页面
    }

    const handleDelete = (row) => {
      // TODO: 调用删除API
    }

    onMounted(() => {
      getList()
    })

    return {
      list,
      total,
      listLoading,
      listQuery,
      statusOptions,
      handleFilter,
      handleCreate,
      handleUpdate,
      handleDelete
    }
  }
})
</script>